package jml.defaultimple.web;

import java.util.List;

import jml.config.Config;
import jml.config.LabelGroups;
import jml.config.ReservedAtts;
import jml.config.Tooltip;
import jml.def.Filter;
import jml.def.Label;
import jml.def.Library;
import jml.def.Title;
import jml.defaultimple.Sublabel;
import jml.defaultimple.util.JMLLoader;
import jml.defaultimple.util.UString;

public class WebFacade
{
	private Library library;
	private String webappRoot;
	
	public WebFacade(String webappRoot,Library library)
	{
		this.library=library;
		this.webappRoot=webappRoot;
	}
	
	public String getLabelInfo(Filter filter,Label label)
	{		
		//String sublabels[] = label.getSublabels();
		
		Sublabel slbl[] = Config.getConfig(false).getLabelGroups().getSublabels(filter.getName(), label.getName());
		
		String ret = "";
		if( slbl!=null && slbl.length>0 ) 
		{
			ret = "(";
			for(int i=0; i<slbl.length; i++)
			{
				ret+=slbl[i].getTuncatedName()+(i<slbl.length-1?", ":")");
			}
		}
		else
		{
			ret= "";
		}
		
		return ret;
	}
	
	public String getTooltip(Title t)
	{
		Tooltip toolTip = Config.getConfig(false).getTooltip(); 
		String exp = toolTip.getExpression();
		
		Filter filters[] = library.getFilters();
		
		for(Filter filter:filters)
		{
			exp = _replaceTooltipExp(exp,t, filter.getName());
		}
		
		ReservedAtts ra = Config.getConfig(false).getReservedAtts();
		
		exp = _replaceTooltipExp(exp,t,ra.getIdAtt());
//		exp = UString.replace(exp,"[br]", "\n");
		exp = UString.replace(exp,"[", "<");
		exp = UString.replace(exp,"]", ">");
		
		return exp;
	}
	
	private String[] _getLink(String attname,String labels[],String realLabels[])
	{
		String ret[] = new String[labels.length];
		for(int i=0; i<labels.length; i++)
		{
			ret[i]="<a href=\"javascript:openArtist('"+attname+"', '"+realLabels[i]+"')\"><b>"+labels[i]+"</b></a>";
		}
		return ret;
	}
	
	
	
	private String _replaceTooltipExp(String exp, Title t, String attname)
	{
		if(exp.indexOf(attname) >= 0)
		{
			String labels[] = t.getLabels(attname);
			String realLabels[] = _obtenerLabelsNoSublabels(t,attname);
			String vals[] = _getLink(attname,labels,realLabels);
			String val = UString.join(vals,',');
			if( val != null )
			{
				// attname=Genre; val=Blues
				if( !attname.equals(Config.getConfig(false).getReservedAtts().getIdAtt()))
				{
					String toReplace = val;
					exp = UString.replace(exp, "$" + attname, toReplace);
				}
				else
				{
//					exp = UString.replace(exp, "$" + attname, val);
					exp = UString.replace(exp, "$" + attname, UString.join(t.getLabels(attname),','));
				}
			}
			else
			{
				exp = UString.replace(exp, "$" + attname, "");
			}	
		}

		return exp;
	}	
	
//	public boolean aplicarTooltip(String coverFullname)
//	{
//		Config config = Config.getConfig(false);
//		
//		if( config.getTooltip().getApplyOn().equals(Tooltip.APPLY_ON_NO_COVER ) )
//		{
//			return coverFullname.endsWith(config.getCovers().getDefault());	
//		}
//		else
//		{
//			return true;
//		}
//	}
	
	private String _obtenerLabelNoSublabel(Title t,String label, String filter)
	{
		// Ej: 
		// filter: "Artist"
		// label: "La Maquina de Hacer Pajaros
		
		LabelGroups lg = Config.getConfig(false).getLabelGroups();
		
		String realLabel = lg.getParent(filter, label);
		
		return realLabel!=null?realLabel:label;
	}
	
	private String[] _obtenerLabelsNoSublabels(Title t, String attname)
	{
		// attname: "Artist"
		
		// obtengo los labels del titulo para "Artist" (Ej. "Charly Garcia")
		String labels[] = t.getLabels(attname);
		
		// los labels que son sublabels los reemplazo por su padre
		String[] ret = new String[labels.length];
		
		int i=0;
		for(String lbl:labels)
		{
			ret[i++] = _obtenerLabelNoSublabel(t, lbl, attname);
		}
		
		return ret;
	}
	
	

	public static void main(String[] args)
	{
		Library lib = JMLLoader.load("WebContent/jmusiclibrary.cfg.xml");

		Filter f = lib.getFilter("Genre");
		Label lb = f.getLabel("Blues");
		
		List<Title> t = lb.getTitles();
		
		WebFacade wf = new WebFacade(".",lib);
		String x = wf.getTooltip(t.get(0));
	}
	
	
}
